package org.swim.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.swim.entity.Ability;
import org.swim.entity.User;
import org.swim.session.AbilityManagerRemote;
import org.swim.session.SearchManagerRemote;

/**
 * Servlet implementation class Search
 */
public class Search extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Search() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
        session.setAttribute("page", "search");
		if(request.getParameter("param").equals("Name"))
		{
			request.getRequestDispatcher("WEB-INF/SearchByName.jsp").forward(request, response);
		} else {
			InitialContext ic;
			try {
				ic = new InitialContext();
				AbilityManagerRemote abilityManager = (AbilityManagerRemote)ic.lookup("AbilityManagerRemote");
				List<Ability> abilities = abilityManager.getAllDefinedAbility();
				if (abilities.size()==0) {
					request.setAttribute("areThereAbilities", false);
				} else {
					request.setAttribute("areThereAbilities", true);
				}
				request.setAttribute("abilities", abilities);
			} catch (NamingException e) {
				e.printStackTrace();
			}
			request.getRequestDispatcher("WEB-INF/SearchByAbility.jsp").forward(request, response);
		}	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
		String inputString = request.getParameter("searchName");
		String parameter = request.getParameter("param");
		if (parameter.equals("ability")) {
			InitialContext ic;
			try {
				ic = new InitialContext();
				AbilityManagerRemote abilityManager = (AbilityManagerRemote)ic.lookup("AbilityManagerRemote");
				List<Ability> abilities = abilityManager.getAllDefinedAbility();
				if (abilities.size()==0) {
					request.setAttribute("areThereAbilities", false);
				} else {
					request.setAttribute("areThereAbilities", true);
				}
				request.setAttribute("abilities", abilities);
			} catch (NamingException e) {
				e.printStackTrace();
			}
			request.getRequestDispatcher("WEB-INF/SearchByAbility.jsp").forward(request, response);
		} else {
			InitialContext ic;
			try {
				ic = new InitialContext();
				SearchManagerRemote searchManager = (SearchManagerRemote)ic.lookup("SearchManagerRemote");
				ArrayList<User> results = (ArrayList<User>) searchManager.getSearchByNameResult(inputString);
				request.setAttribute("searchResults", results);
				request.getRequestDispatcher("WEB-INF/SearchByName.jsp").forward(request, response);
			} catch (NamingException e) {
				request.getRequestDispatcher("WEB-INF/SearchByName.jsp").forward(request, response);
				e.printStackTrace();
			}
		}
	}

}
